<?php 
namespace app\admin\controller;
use think\Db;//导入数据库类 (DB类)
use think\Controller;
use think\Session;
use think\Request;
use app\admin\model\User;
class Notice extends Controller
{

    public function noti()
    {
        // if(input('get.a') == 1){
        //     db('notice')->where('id',input('id'))->update(['status'=>0]);
        // }else{
        //     db('notice')->where('id',input('id'))->update(['status'=>1]);
        // }
        // $list = db('link')->select();
        $f = db('notice')->where('id',input('id'))->select();
        $face = $f[0]['status'];

        if($face == 1){
            return '<script>alert("已经是公告啦");window.location="index"</script>';
        }else{
            db('notice')->where('test',1)->setField('status',0);
            db('notice')->where('id',input('id'))->setField('status',1);
            $this->redirect('Home/index');
        }
    }

    public function index()
    {
        $list = db('notice')->select();
        $this->assign('notic',$list);
        $no = db('notice')->where('status',1)->select();
        $this->assign('no',$no);

        return view('notice/admin-notice');

    }

    public function del()
    {
         if(Request::instance()->isPost()){
            if(db('notice')->delete(input('post.id'))){
                return '删除成功';
            }else{
                return '删除失败';
            }
        }
    }

    // 编辑修改
    public function edi()
    {
         $no = db('notice')->where('status',1)->select();
        $this->assign('no',$no);
        $edi = db('notice')->where('id',input('id'))->select();
        $this->assign('data',$edi);
        return view('notice/edit');
    }
    // 执行编辑
    public function edit()
    {
        if(Request::instance()->isPost()){

                if( db('notice')->where('id',input('id') )->update(input())){
                    $this->success('修改成功','index');
                }else{
                    $this->error('修改失败');
                }
        
            
        }
    }
    // 添加
    public function ad()
    {
        return view('add');
    }

    // 执行添加
    public function add()
    {
        // return json_encode(input('post.'));
        if(Request::instance()->isPost()){

            if(input('post.status') == 1){
                db('notice')->where('test',1)->setField('status',0);
                 if(db('notice')->insert(input())){
                    return 'ok';
                 }else{
                    return 'no';
                 }
            }else{
                if(db('notice')->insert(input())){
                    return 'ok';
                 }else{
                    return 'no';
                 }
            }
            
        }
    }
}